package com.dks.controller;


import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;

import com.dks.businessUtil.RewardUtil;
import com.dks.exception.BaseException;
import com.dks.result.Result;
import com.dks.result.ResultInfo;
import com.dks.result.ResultModel;
import com.dks.util.AWSFileUploadUtil;
import com.dks.util.CreateQRCodeUtil;
import com.dks.util.QiniuCloudUtil;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("aws")
public class FileAwsController {


    /**
     * @Author: Ah Nan
     * @Description:获取aws上传地址
     * @Param: tel
     * @return: ResultModel
     * @Date: 2020/4/19
     */
    @PostMapping("get-upload-file-url")
    public ResultModel<Object> getUploadFileUrl(MultipartFile file) throws IOException {

        AWSFileUploadUtil awsFileUploadUtil = new AWSFileUploadUtil();

        URL url = awsFileUploadUtil.AWSGetUrl(file);

        return Result.makeResult(ResultInfo.SUCCESS, url);
    }

    /**
     * @Author: Ah Nan
     * @Description:调用七牛云上传文件
     * @Param: multipartFile
     * @return: ResultModel
     * @Date: 2020/4/19
     */
    @PostMapping("upload-upfile")
    public ResultModel<Object> uploadUpfile(@RequestParam("file") MultipartFile multipartFile) throws IOException, BaseException {

        //uploadFile是MultipartFile对象
        QiniuCloudUtil qiniuCloudUtil = new QiniuCloudUtil();
        //MultipartFile转换为file
        File file = qiniuCloudUtil.convertMultipartFile(multipartFile);
        String fileName = (UUID.randomUUID().toString().replace("-", "").substring(0, 12) + "." + file.getName().substring(file.getName().lastIndexOf(".") + 1));

        //文件的url
        String url = QiniuCloudUtil.upload(file, fileName);

        if (url == null || url.isEmpty()) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, multipartFile.getOriginalFilename());
        }

        return Result.makeResult(ResultInfo.SUCCESS, url);
    }


    /**
     * @Author: Ah Nan
     * @Description:获取qrCodeUrl
     * @Param: tel
     * @return: ResultModel
     * @Date: 2020/4/19
     */
    @GetMapping("get-qr-code-url")
    public ResultModel<Object> getQrCodeUrl(Integer id) throws Exception {

        if (id == null ) {
            return Result.makeResult(ResultInfo.ERR_PARAM_NULL, id);
        }
        CreateQRCodeUtil createQRCodeUtil = new CreateQRCodeUtil();
        String url = createQRCodeUtil.CreateQRCode(id);
        return Result.makeResult(ResultInfo.SUCCESS, url);
    }


}
